package com.example.douyin.util;

import cn.hutool.core.io.FileUtil;
import org.apache.log4j.Logger;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    /**
     * 通过Html中元素获取对应的值
     *
     * @param source
     * @return
     */
    public static final String[] SUFFIX = {".mp3", ".mp4"};


    public static List match(String source) {

        List result = new ArrayList();

        String mailreg = "<a(?:\\s+.)*?\\s+href=\"([^\"]*?)\"";

        Matcher m = Pattern.compile(mailreg).matcher(source);

        while (m.find()) {

            String r = m.group(1);

            result.add(r);

        }
        return result;

    }

    /**
     * 之前固定下载的通用方法
     *
     * @param inStream
     * @param itemId
     */
    public static void downloadMusic(InputStream inStream, String itemId) {

        Logger logger = Logger.getLogger(CommonUtil.class);

        int byteRead;

        try {

            //封装一个保存文件的路径对象

            File fileSavePath = new File(PathUtil.getCommonPath("SanMu" + File.separator + "Music" + File.separator + itemId + SUFFIX[0]));

            //注:如果保存文件夹不存在,那么则创建该文件夹

            if(!fileSavePath.exists()) {
                fileSavePath.getParentFile().mkdirs();
            }

            File touch = FileUtil.touch(fileSavePath);

            //写入文件

            FileOutputStream fs = new FileOutputStream(touch);

            byte[] buffer = new byte[1024];

            while ((byteRead = inStream.read(buffer)) != -1) {

                fs.write(buffer, 0, byteRead);

            }

            inStream.close();

            fs.close();

            System.out.println("\n-----Music保存路径-----\n" + fileSavePath.getAbsolutePath());

        } catch (FileNotFoundException e) {
            logger.info(e);
        } catch (IOException e) {
            logger.info(e);
        }


    }

    /**
     * 对应的固定的逻辑下载的video的方法
     *
     * @param videoAddress
     * @param itemId
     */

    public static void downloadVideo(String videoAddress, String itemId) {

        Logger logger = Logger.getLogger(CommonUtil.class);

        int byteRead;

        try {

            URL url = new URL(videoAddress);

            //获取链接

            URLConnection conn = url.openConnection();

            //输入流

            InputStream inStream = conn.getInputStream();

            //封装一个保存文件的路径对象

            File fileSavePath = new File(PathUtil.getCommonPath("SanMu" + File.separator + itemId + SUFFIX[1]));

            //注:如果保存文件夹不存在,那么则创建该文件夹

            File fileParent = fileSavePath.getParentFile();

            if (!fileParent.exists()) {

                fileParent.mkdirs();

            }

            //写入文件

            FileOutputStream fs = new FileOutputStream(fileSavePath);

            byte[] buffer = new byte[1024];

            while ((byteRead = inStream.read(buffer)) != -1) {

                fs.write(buffer, 0, byteRead);

            }

            inStream.close();

            fs.close();

            System.out.println("\n-----视频保存路径-----\n" + fileSavePath.getAbsolutePath());

        } catch (FileNotFoundException e) {
            logger.info(e);

        } catch (IOException e) {
            logger.info(e);
        }

    }
}
